package command;

import util.Customer;
import util.CustomerStorage;
import util.IO;

public class LoginCommand implements Command {
    private String libraryNumber;
    private String password;
    private Customer customer;

    public LoginCommand() {

    }

    public LoginCommand(String libraryNumber, String password) {
        this.libraryNumber = libraryNumber;
        this.password = password;
    }

    @Override
    public void run() {
        if (login())
            welcomeMessage();
        else
            reloginMessage();
    }

    private void reloginMessage() {
        IO.output("Invalid library number and password.Please try again.");
    }

    private void welcomeMessage() {
        IO.output("Welcome, " + customer.getName() + "\n");
    }

    private boolean login() {
        Customer customer = CustomerStorage.get(libraryNumber);
        if (customer != null && customer.getPassword().equals(password)) {
            CustomerStorage.setLoginCustomer(customer);
            this.customer = customer;
            return true;
        } else
            return false;
    }
}
